package com.hzhr.iot.meterplan.service.base;

import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.constant.HzhrAttrNames;
import com.hzhr.iot.core.constant.HzhrHeaderNames;
import com.hzhr.iot.log.sdk.model.log.LogModel;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.meterplan.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Component
public class BaseService {
    @Autowired
    HttpServletRequest request;

    @Autowired
    Global global;

    protected UserCardModel getUserCard(Map<String, String> header) {
       if (header == null || StringUtils.isEmpty(header.get(HzhrHeaderNames.USER_ID))) {
           return null;
       }

        UserCardModel userCard = new UserCardModel();
        userCard.setUserId(Integer.parseInt(header.get(HzhrHeaderNames.USER_ID)));
        userCard.setUserCode(header.get(HzhrHeaderNames.USER_CODE));
        userCard.setUserName(header.get(HzhrHeaderNames.USER_NAME));
        userCard.setCompanyId(Integer.parseInt(header.get(HzhrHeaderNames.COMPANY_ID)));
        userCard.setDeptId(Integer.parseInt(header.get(HzhrHeaderNames.DEPT_ID)));
        userCard.setType(header.get(HzhrHeaderNames.USER_TYPE));

        return userCard;
    }
    protected Integer getUserId(Map<String, String> header) {
        UserCardModel userCard = getUserCard(header);
        Integer userId = userCard.getUserId();
        return userId;
    }
    protected String getUserName(Map<String, String> header) {
        UserCardModel userCard = getUserCard(header);
        String userName = userCard.getUserName();
        return userName;
    }
    protected String getUserCode(Map<String, String> header) {
        UserCardModel userCard = getUserCard(header);
        String userCode = userCard.getUserCode();
        return userCode;
    }
    protected Integer getCompanyId(Map<String, String> header) {
        UserCardModel userCard = getUserCard(header);
        Integer companyId = userCard.getCompanyId();
        return companyId;
    }
    protected Integer getDeptId(Map<String, String> header) {
        UserCardModel userCard = getUserCard(header);
        Integer deptId = userCard.getDeptId();
        return deptId;
    }
    protected String getUserType(Map<String, String> header) {
        UserCardModel userCard = getUserCard(header);
        String userTypeCode = userCard.getType();
        return userTypeCode;
    }
    protected boolean isInternalServiceInvok(Map<String, String> header) {
        String token = header.get("_token");
        if (StringUtils.isEmpty(token)) return false;
        String[] tokenSplit = global.getToken().split("_");
        for (String item:
                tokenSplit) {
            if (item.equals(token)) {
                return true;
            }
        }
        return false;
    }
    protected boolean isGlobal(Map<String, String> header) {
        if ("0".equals(header.get(HzhrHeaderNames.USER_ID)) || "1".equals(header.get(HzhrHeaderNames.ACCESS_MODE))) {
            return true;
        } else {
            return false;
        }
    }

    protected LogModel getLog() {
        LogModel log = (LogModel) request.getAttribute(HzhrAttrNames.LOG);
        return log;
    }

    protected Map<String, String> initHeader(Map<String, String> header) {
        Map<String, String> newHeader = new HashMap<>();
        newHeader.putAll(header);
        newHeader.put("token", global.getFirstToken());
        newHeader.put(HzhrHeaderNames.ACCESS_MODE, AccessModeEnum.NORMAL.getCode().toString());
        return newHeader;
    }

    protected Map<String, String> initHeader(Map<String, String> header, AccessModeEnum accessMode) {
        Map<String, String> newHeader = new HashMap<>();
        newHeader.putAll(header);
        newHeader.put("token", global.getFirstToken());
        newHeader.put(HzhrHeaderNames.ACCESS_MODE, accessMode.getCode().toString());
        return newHeader;
    }

    protected String getTransactionId(Map<String, String> header) {
        String transactionId = header.get(HzhrHeaderNames.TRANSACTION_ID);
        return transactionId;
    }
}
